﻿(function($) {//知识学习
    $.fn.learn = function(settings) {
        var config = $.extend(settings);
        if (config.questions === null) {
            $(this).html('<div class="intro-container slide-container"><h2 class="qTitle">Failed to parse questions.</h2></div>');
            return;
        }
        var superContainer = $(this),
        answers = [],
		questionNum = '',
		questionInfo = '',
		questionBtn = '',
		questionAnswers = '',
        questionsIteratorIndex,
        answersIteratorIndex;
        superContainer.addClass('main-quiz-holder');
		questionsIteratorIndex=0;
		init(0);
		
		function init(index){
			questionAnswers = '',
			questionNum = '习题'+(index+1);//当前习题数
			questionInfo = config.questions[index].question;//习题内容
			if(index<config.questions.length-1){
				questionBtn = '<button class="next blend-button blend-radius pull-right">下一题</button>';
			}else{
				questionBtn = '<button class="final blend-button blend-radius pull-right jump_url">没有了</button>';
				setTimeout(function(){
					dialog.dialog("show");
					//window.location.href= sessionStorage.jump_url;
				},1000);
				
			}
			for (answersIteratorIndex = 0; answersIteratorIndex < config.questions[index].answers.length; answersIteratorIndex++) {//习题答案
				if(answersIteratorIndex==(config.questions[index].correctAnswer-1)){
					questionAnswers += '<div class="blend-formgroup"><input type="text" class="blend-formgroup-input right" value="' + config.questions[index].answers[answersIteratorIndex] + '" readonly="readonly" autocomplete="off"/></div>';
				} else{
					questionAnswers += '<div class="blend-formgroup"><input type="text" class="blend-formgroup-input" value="' + config.questions[index].answers[answersIteratorIndex] + '" readonly="readonly" autocomplete="off"/></div>';
				}
			}
			superContainer.find('.question-number').html(questionNum);
			superContainer.find('.question-info').html(questionInfo);
			superContainer.find('.question-btn').html(questionBtn);
			superContainer.find('.answers').html(questionAnswers);
			
			superContainer.find('.next').click(function() {	
				index = ++questionsIteratorIndex;
				$(".answers").html("");
				if(index<config.questions.length){
					init(index);
				}

				return false;
			});
			
		}
		
			


    };
})(Zepto);

(function($) {//考试
    $.fn.answer = function(settings) {
        var config = $.extend(settings);
        if (config.questions === null) {
            $(this).html('<div class="intro-container slide-container"><h2 class="qTitle">Failed to parse questions.</h2></div>');
            return;
        }
        var superContainer = $(this),
        answers = [],
		questionNum = '',
		questionInfo = '',
		questionAnswers = '',
		userAnswers = [],
        questionsIteratorIndex,
        answersIteratorIndex;
        superContainer.addClass('main-quiz-holder');
		questionsIteratorIndex=0;
		init(0);
		
		function init(index){
			questionAnswers = '',
			questionNum = '习题'+(index+1);//当前习题数
			questionInfo = config.questions[index].question;//习题内容
			if(index<config.questions.length-1){
				questionBtn = '<button class="next blend-button blend-radius pull-right">下一题</button>';
			}else{
				questionBtn = '<button class="final blend-button blend-radius pull-right">没有了</button>';
			}
			for (answersIteratorIndex = 0; answersIteratorIndex < config.questions[index].answers.length; answersIteratorIndex++) {//习题答案
					questionAnswers += '<div class="blend-formgroup"><input type="text" class="blend-formgroup-input" value="' + config.questions[index].answers[answersIteratorIndex] + '" readonly="readonly" autocomplete="off"/></div>';
			}
			superContainer.find('.question-number').html(questionNum);
			superContainer.find('.question-info').html(questionInfo);
			superContainer.find('.answers').html(questionAnswers);
			
			superContainer.find('.blend-formgroup-input').click(function() {
				var thisLi = $(this);	
				if(!thisLi.hasClass('right')){
					var userselected=($(this).parents('.blend-formgroup').index()+1);//用户当前选中的答案
					var current=questionsIteratorIndex;//当前问题为所有问题中第几个
					if(userselected==config.questions[current].correctAnswer){//用户当前选中的答案与正确答案是否匹配
						current = ++questionsIteratorIndex;
						thisLi.parents('.answers').children('.blend-formgroup').find('.blend-formgroup-input').removeClass('right');
						thisLi.parents('.answers').children('.blend-formgroup').find('.blend-formgroup-input').removeClass('wrong');
						thisLi.addClass('right');
						thisLi.parents('.questions').find('.bird2').addClass('right');
						setTimeout(function(){thisLi.parents('.questions').find('.bird2').removeClass('right');},1000);
						if(current<config.questions.length){
							setTimeout(function(){init(current);},1000);//1秒后跳到下一题
						}else{
							setTimeout(function(){
								$.ajax({
									type:'post',
									url:'modify_member_isexam',
									data:{},
									dataType:'json',
									success:function(data){
										if(data===1){
											window.location.href="exam_result";
										}
									}
								});
							},1000);
						}
					}else{
						thisLi.parents('.answers').children('.blend-formgroup').find('.blend-formgroup-input').removeClass('right');
						thisLi.parents('.answers').children('.blend-formgroup').find('.blend-formgroup-input').removeClass('wrong');
						thisLi.addClass('wrong');
						thisLi.parents('.questions').find('.bird2').addClass('wrong');
						setTimeout(function(){thisLi.removeClass('wrong');thisLi.parents('.questions').find('.bird2').removeClass('wrong');},1000);//1秒后错误提示信息消失
					}
				}			
			});
			
		}
		
			


    };
})(Zepto);